package com.orcamento.dao;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.orcamento.entity.Installment;

public class InstallmentDao extends AbstractDao<Installment> {

	public InstallmentDao() {
		super(Installment.class);
	}

	@Override
	public Installment findByIdAndAccount(Long id, Long accountId) {
		Criteria criteria = session().createCriteria( Installment.class );
		criteria.createAlias("spent", "spent", JoinType.INNER_JOIN);
		criteria.add( Restrictions.eq("id", id) );
		criteria.add( Restrictions.eq("spent.account.id", accountId) );
		return (Installment) criteria.uniqueResult();
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public Installment save(Installment installment) {
		return saveEntity(installment);
	}

}
